package application;

import com.example.menhoo.toollibrary.common.assist.Check;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yanglei on 15/9/22.
 */
public class UIHttpURL {

    private static String web_site;
    private static String web_site2;
    private static String ip;

    /**
     * 设置app的Http接口的主网站
     * @param IPAndPort
     */
    public static void setWebSite(String IPAndPort) {
        if(IPAndPort.indexOf("http://")!=-1 || IPAndPort.indexOf("https://")!=-1 || IPAndPort.indexOf("file://")!=-1){

            web_site=IPAndPort;
        }
        else{
            IPAndPort=IPAndPort.replace("http://", "");
            web_site="http://"+IPAndPort;
        }

    }
    /**
     * 设置app的Http接口的主网站
     * @param IPAndPort
     */
    public static void setWebSite2(String IPAndPort) {
        if(IPAndPort.indexOf("http://")!=-1 || IPAndPort.indexOf("https://")!=-1 || IPAndPort.indexOf("file://")!=-1){

            web_site2=IPAndPort;
        }
        else{
            IPAndPort=IPAndPort.replace("http://", "");
            web_site2="http://"+IPAndPort;
        }

    }

    public static String getWebSiteFullUrl2() {
        return web_site2;
    }

    /**
     * 返回完整的url website+path
     * @param path http 接口片段(一般是此类或者子类的静态字段)
     * @return
     */
    public static String getWebSiteFullUrl(String path) {
        if(path.indexOf("http://")!=-1 || path.indexOf("https://")!=-1 || path.indexOf("file://")!=-1){
            return path;
        }
        else{
            if(Check.isEmpty(web_site)){
                return path;
            }
            else{
                if(path.startsWith("/")){
                    return web_site+path;
                }
                return web_site+"/"+path;
            }
        }

    }
    public static String getWebSiteFullUrl(){
        return web_site;
    }

    /**
     * 返回带参数的完整的url website+path?canshu1=zhi1&canshu2=zhi2
     * @param path http 接口片段(一般是此类或者子类的静态字段)
     * @param params 参数键值对
     * @return
     */
    public static String getWebSiteFullUrl(String path,Map<String, String> params)
    {
        String url_ =getWebSiteFullUrl(path);

        if(url_.indexOf("file://")==-1){
            if(params!=null && params.size()>0)
            {
                String str_pram="";

                for(String key:params.keySet())
                {
                    str_pram+=key+"="+params.get(key);
                    str_pram+="&";
                }
                int len = path.lastIndexOf("?");

                if( len<0){
                    url_+="?"+str_pram;
                }
                else{
                    if(url_.endsWith("&")){
                        url_+=str_pram;
                    }
                    else{
                        url_+="&"+str_pram;
                    }
                }

            }
            if(url_.endsWith("&")){
                url_=url_.substring(0,url_.length()-1);
            }
        }

        return url_;
    }

    /**
     * 返回带参数的完整的url path?canshu1=zhi1&canshu2=zhi2
     * @param path http 接口片段(一般是此类或者子类的静态字段)
     * @param params 参数键值对
     * @return
     */
    public static String getUrl(String path,Map<String, String> params)
    {
        String url_ =path;

        if(params!=null && params.size()>0)
        {
            String str_pram="";

            for(String key:params.keySet())
            {
                str_pram+=key+"="+params.get(key);
                str_pram+="&";
            }
            int len = path.lastIndexOf("?");

            if( len<0){
                url_+="?"+str_pram;
            }
            else{
                if(url_.endsWith("&")){
                    url_+=str_pram;
                }
                else{
                    url_+="&"+str_pram;
                }
            }

        }
        if(url_.endsWith("&")){
            url_=url_.substring(0,url_.length()-1);
        }
        return url_;
    }

    /**
     * 获得fullUrl中的参数
     * @param fullUrl 完整的url
     * @return
     */
    public static Map<String, String> getUrlParameters(String fullUrl)
    {
        Map<String, String> parmMap =null;
        int startIndex = fullUrl.indexOf("?");
        if(startIndex>=0)
        {
            parmMap = new HashMap<String, String>();
            String parmStr = null;
            try {
                parmStr = fullUrl.substring(startIndex+1);
            } catch (Exception e) {
                return null;
            }
            String[] parm_s =parmStr.split("&");
            for(int i=0;i<parm_s.length;i++)
            {
                String temp_parm = parm_s[i];

                if(temp_parm!=null)
                {
                    String[] parm_ = temp_parm.split("=");
                    if(parm_.length==2)
                    {
                        parmMap.put(parm_[0], parm_[1]);
                    }
                    else if(parm_.length==1)
                    {
                        parmMap.put(parm_[0], "");
                    }

                }

            }

            if(parmMap.size()>0)
            {
                return parmMap;
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }

    }
}
